
//
//  SWVisitorView.swift
//  STslender
//
//  Created by mac on 15/12/6.
//  Copyright © 2015年 itcast. All rights reserved.

 /// 访客页面

import UIKit

class SWVisitorView: UIView {

    override init(frame: CGRect) {
       super.init(frame: frame)
       setUpUI()
        

    }
    
    
    required init?(coder aDecoder: NSCoder) {
      
        fatalError("init(coder:) has not been implemented")
    }
  
// 供外界调用,设置 对应控制器 图标以及消息文字的方法
    //设置不同控制器的 图标
    func  setshowVisitorMessage(imgName:String? ,message:String?){
       
         // 使用 if let 判断一个可选项是否有值,如果有值,使用一个常量记录其的值
        if let  img = imgName, msg = message {
            
            //影藏 圈圈
            circleView.hidden = true
            
            iconView.image = UIImage(named: img)
            messageLable.text = msg
            
        }else{
            startAnimation()
        }
        
    }
    
    // 设置圈圈动画
    private func startAnimation(){
        //初始化动画为 核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")

        animation.toValue = 2 * M_PI
        animation.duration = 20
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        // 在切换 tabbar 或者 将应用切换到后台, anim 会被释放
         animation.removedOnCompletion = false
   
       circleView.layer.addAnimation(animation, forKey: nil)
    }
    

    // 懒加载
    private lazy var iconView :UIImageView = {
        
        let img = UIImage(named: "visitordiscover_feed_image_house")
        let imgView = UIImageView(image: img)
        return imgView
        
        }()
    
   private lazy var circleView: UIImageView = {
        let img = UIImage(named: "visitordiscover_feed_image_smallicon")
        let imgView  = UIImageView(image: img)
      return  imgView
        
    }()
   
    private lazy var messageLable: UILabel = {
        let lable = UILabel()
        // 设置颜色以及字体大小
       lable.textColor = UIColor.darkGrayColor()
       lable.font = UIFont.systemFontOfSize(14)
        lable.text = "关注一些人，回这里看看有什么惊喜"
        lable.numberOfLines = 0
        return lable
        
        }()
    
     lazy var registerButton: UIButton = {
       let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
     lazy var  loginButton: UIButton = {
        let button = UIButton()
        
         button.setTitle("登陆", forState: UIControlState.Normal)
        
         button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
         button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
          button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    private lazy var  maskIconView: UIImageView = {
         let  img = UIImage(named: "visitordiscover_feed_mask_smallicon")
         let  imgView = UIImageView (image: img)
        
         return imgView
    }()
  

}

extension SWVisitorView{
    // 设置视图
    //Protected的方法可以在其定义的类中或子类中使用，可以指定接受者（作为方法method）或者不指定接收者（函数function）使用
    private func setUpUI(){
        
        //设置view的背景颜色 与 阴影 色一样
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加视图
        // 首页的圆
        addSubview(circleView)
        // 阴影
        addSubview(maskIconView)
        //图标
        addSubview(iconView)
        // 显示消息的 label
        addSubview(messageLable)
        // 注册按钮
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        
        //给view添加约束
        //1.iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //布局
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //2.circleView
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 3.messageLable
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item:messageLable, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        //4 registerButton
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute:
            .Leading, relatedBy: .Equal, toItem: messageLable, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //5 loginButton
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLable, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem:nil , attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //6  maskIconView// VFL方式
        
        // H: 水平方向
        // |: 边缘
        // []: 里面放的是 View -> views 设置字典用
        // (): 里面放的是数值 --> metrics 设置数值字典
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(offset)-[registerButton]", options: [], metrics: ["offset" : -35], views: ["maskIconView": maskIconView,"registerButton": registerButton]))
    }
}
